/**
 * jims
 */
package com.jims.master.modules.blood.web;


import com.jims.master.common.persistence.Page;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.blood.entity.BloodApply;
import com.jims.master.modules.blood.service.BloodApplyService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 用血申请Controller
 * @author CTQ
 * @version 2017-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/blood/bloodApply")
public class BloodApplyController extends BaseController {

	@Autowired
	private BloodApplyService bloodApplyService;

	@ModelAttribute
	public BloodApply get(@RequestParam(required=false) String id) {
		BloodApply entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bloodApplyService.get(id);
		}
		if (entity == null){
			entity = new BloodApply();
		}
		return entity;
	}

    /**
     * 用血申请主页面
     * @param bloodApply
     * @param model
     * @return
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(BloodApply bloodApply, Model model) {
		model.addAttribute("bloodApply",bloodApply);
		return "modules/blood/bloodIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"list", ""})
	public String list(BloodApply bloodApply, HttpServletRequest request, HttpServletResponse response, Model model) {
        bloodApply.setCreateBy(UserUtils.getUser());
		Page<BloodApply> page = bloodApplyService.findPage(new Page<BloodApply>(request, response), bloodApply);
		model.addAttribute("page", page);
		return "modules/blood/bloodApplyList";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(BloodApply bloodApply, Model model) {
		bloodApply.setCreateDate(new Date());
		model.addAttribute("bloodApply", bloodApply);
		return "modules/blood/bloodApplyForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(BloodApply bloodApply, Model model) {
	    StringData data=new StringData();
		int i=bloodApplyService.saveApply(bloodApply);
        if(i>0){
            data.setCode("success");
            data.setData("保存用血申请成功");
        }else{
            data.setCode("error");
            data.setData("保存用血申请失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(BloodApply bloodApply) {
	    StringData data=new StringData();
		int i=bloodApplyService.deleteApply(bloodApply);
        if(i>0){
            data.setCode("success");
            data.setData("删除用血申请成功");
        }else{
            data.setCode("error");
            data.setData("删除用血申请失败");
        }
		return data;
	}

    /**
     * 用血申请单打印
     * @param applyNum
     * @return com.jims.common.data.StringData
     * @author fengyg
     * @date 2017-07-26
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "printBloodApply")
    @ResponseBody
    public StringData printBloodApply(String applyNum){
        StringData stringData = new StringData();
        BloodApply bloodApply = new BloodApply();
        bloodApply.setApplyNum(applyNum);
        String printPath = bloodApplyService.printBloodApply(bloodApply);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
}